package com.skycity.auth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
//@EnableWebSecurity
public class SecurityConfig {
    @Bean
    SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
        http
//                .authorizeHttpRequests(authorizeRequests ->
//                        authorizeRequests.requestMatchers("/login","/logout").permitAll()
//                                .anyRequest().permitAll()
//                )
                .authorizeHttpRequests(authorizeRequests ->
                        authorizeRequests
//                        .requestMatchers("/oauth2/**").permitAll()
                                .anyRequest().authenticated()
                )
//                .addFilterBefore(new Oauth2TokenInfoEndPointFilter(), AuthorizationFilter.class)
                .logout(AbstractHttpConfigurer::disable)
                .sessionManagement(AbstractHttpConfigurer::disable)
                .httpBasic(AbstractHttpConfigurer::disable)
                .csrf(AbstractHttpConfigurer::disable)
                .anonymous(AbstractHttpConfigurer::disable)
                .formLogin(Customizer.withDefaults())
//                .formLogin(form->form.loginPage("/login")
                ;
        return http.build();
    }

    //http://localhost:7001/oauth2/authorize?response_type=code&client_id=oidc-client&redirect_uri=http://127.0.0.1:7001/oauth2/code&scope=profile

    //http://localhost:7001/oauth2/token?client_id=oidc-client&client_secret=secret&grant_type=authorization_code&redirect_uri=http://127.0.0.1:8080/login/oauth2/code/oidc-client&code=nFD-SYZiCOFQe8U1bSjxzt3nqkb8_G8NgMNlKg7ted3A6v-AfwAqlsOxhk6WS1GLIoLOZw0FMNd1E-05FjTk40Gb-RjIaaoJOOuPqMmtHwHmmSZviZeEZtOsb4axGpE1
    @Bean
    PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

}
